<template>
  <div class="select-area">
    <h5 class="subtitle">第一步</h5>
    <cube-form :model="model" @validate="validateHandler" @submit="submitHandler">
      <cube-form-group>
        <cube-form-item :field="fields[0]">
          <div class="selectedVal" @click="showAddressPicker">
            <span v-if="model.areaValue==''" class="placeholder">请选择所在地区</span>
            <span v-if="model.areaValue!=''">{{model.areaValue}}</span>
          </div>
        </cube-form-item>
      </cube-form-group>
      <cube-form-group class="subBtn">
        <cube-button type="submit" :inline="true" :primary="true">下一步</cube-button>
      </cube-form-group>
    </cube-form>
  </div>
</template>

<script>
import { provinceList, cityList } from "@/assets/js/area.js";

const addressData = provinceList;
addressData.forEach(province => {
  province.children = cityList[province.value];
});

export default {
  name: "SelectArea",
  data() {
    return {
      validity: {},
      valid: undefined,
      model: {
        areaValue: ""
      },
      province: "",
      region: "",
      fields: [
        {
          modelKey: "areaValue",
          label: "选择地区",
          props: {
            placeholder: "请选择所在地区"
          },
          rules: {
            required: true
          },
          messages: {
            required: "请选择所在地区"
          },
          events: {}
        }
      ]
    };
  },
  mounted() {
    this.addressPicker = this.$createCascadePicker({
      title: "选择所在地区",
      data: addressData,
      onSelect: this.selectHandle,
      onCancel: this.cancelHandle
    });
    if (this.$route.query.area) {
      this.model.areaValue = this.$route.query.area;
    }
  },
  methods: {
    showAddressPicker() {
      this.addressPicker.show();
    },
    selectHandle(selectedVal, selectedIndex, selectedText) {
      this.model.areaValue = selectedText.join(" ");
      this.province = selectedVal[0];
      this.region = selectedVal[1];
    },
    cancelHandle() {
      this.$createToast({
        type: "correct",
        txt: "取消选择",
        time: 1000
      }).show();
    },

    validateHandler(result) {
      this.validity = result.validity;
      this.valid = result.valid;
    },
    submitHandler(e) {
      e.preventDefault();
      this.$store.state.province = this.province;
      this.$store.state.region = this.region;
      let requestData = {
        province: this.$store.state.province,
        region: this.$store.state.region
      };
      this.$http
        .post("/api000011", requestData)
        .then(response => {
          if (response.data.status == 0) {
            this.$createToast({
              type: "warn",
              time: 1000,
              txt: response.data.msg
            }).show();
          } else {
            this.$store.state.schools = response.data;
            this.$router.push({
              path: this.$route.path + "/school",
              query: {
                requestData
              }
            });
          }
        })
        .catch(error => {
          this.$createToast({
            type: "error",
            txt: error,
            time: 1000
          }).show();
        });
    }
  }
};
</script>

<style lang="scss">
.select-area {
  .subtitle {
    font-size: 16px;
    font-weight: 700;
    color: #fc9153;
    margin-top: 20%;
    margin-bottom: 10%;
    text-align: center;
  }
  /* //设置存储picker选择获取的值的div的样式 */
  .selectedVal {
    text-align: left;
    /* 设置存储picker选择获取的值的div显示placeholder时字体的颜色 */
    .placeholder {
      color: #ccc;
    }
  }
  .subBtn {
    margin: 5% auto;
    text-align: center;
    margin-top: 20%;
    button {
      width: 40%;
      margin: 2%;
    }
  }
}
</style>